package com.lilanz.microservice.express.utils.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.lilanz.microservice.express.utils.exception.MyException;
import java.io.*;
import java.util.Calendar;
public class StringUtils {

    public static boolean isNotEmpty(String arg0) {
        return arg0 != null && arg0.trim().length() > 0;
    }


    //获取临时路径
    public static String getTemPath(String pdfSuffix) {
        //按年月日分文件夹
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        int day = instance.get(Calendar.DAY_OF_MONTH);
        StringBuilder sb = new StringBuilder();
        sb.append(pdfSuffix).append(year).append("/").append(month).append("/").append(day).append("/");
        File file = new File(sb.toString());
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new MyException("创建临时文件夹失败" + file.toString());
            }
        }
        return sb.toString();
    }

    /**
     * 二维码
     */
    public static boolean twoDimensionCode(String contents, String imgPath) {
        int width = 200, height = 200;
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
            //QR_CODE为二维码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.QR_CODE, codeWidth, height, null);
            //在这里将生成的条形码的图片放在指定的路径下
            MatrixToImageWriter.writeToPath(bitMatrix, "png", new File(imgPath).toPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 条形码
     */
    public static boolean barCode(String contents, String imgPath) {
        int width = 85, height = 40;
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
            //在这里生成条形码的图片
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.CODE_128, codeWidth, height, null);
            //在这里将生成的条形码的图片放在指定的路径下
            MatrixToImageWriter.writeToPath(bitMatrix, "png", new File(imgPath).toPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }


}
